/**
 * Copyright (c) Tiny Technologies, Inc. All rights reserved.
 * Licensed under the LGPL or a commercial license.
 * For LGPL see License.txt in the project root for license information.
 * For commercial licenses see https://www.tiny.cloud/
 *
 * Version: 5.8.2 (2021-06-23)
 */
(function () {
  'use strict';
  var global$1 = tinymce.util.Tools.resolve('tinymce.util.Promise');
  var global$2 = tinymce.util.Tools.resolve('tinymce.Env');
  var global$3 = tinymce.util.Tools.resolve('tinymce.util.Delay');
  var pickFile = function (editor, accept) {
    return new global$1(function (resolve) {
      var fileInput = document.createElement('input');
      fileInput.type = 'file';
      fileInput.accept = accept;
      fileInput.style.position = 'fixed';
      fileInput.style.left = '0';
      fileInput.style.top = '0';
      fileInput.style.opacity = '0.001';
      document.body.appendChild(fileInput);
      var changeHandler = function (e) {
        resolve(Array.prototype.slice.call(e.target.files));
      };
      fileInput.addEventListener('change', changeHandler);
      var cancelHandler = function (e) {
        var cleanup = function () {
          resolve([]);
          fileInput.parentNode.removeChild(fileInput);
        };
        if (global$2.os.isAndroid() && e.type !== 'remove') {
          global$3.setEditorTimeout(editor, cleanup, 0);
        } else {
          cleanup();
        }
        editor.off('focusin remove', cancelHandler);
      };
      editor.on('focusin remove', cancelHandler);
      fileInput.click();
    });
  };
  tinymce.PluginManager.add('fileUpload', function (editor) {
    var uploadImageFun = editor.getParam('images_upload_handler');
    var uploadFileFun = editor.getParam('uploadFile');
    // 注册一个工具栏按钮名称
    // 图片上传
    editor.ui.registry.addButton('imageupload', {
      icon: 'image',
      tooltip: 'Insert image',
      onAction: function () {
        var fileTypeLimit = (editor.getParam('fileTypeLimit') && editor.getParam('fileTypeLimit')['image']) || "image/*"
        pickFile(editor, fileTypeLimit).then(function (files) {
          if (files.length > 0) {
            uploadImageFun(files[0], function(res) {
              editor.insertContent(editor.dom.createHTML('img', { src: res }));
            }, function(message) {
              editor.notificationManager.open({
                text: message,
                type: 'error',
            });
            })
          }
        });
      }
    });
    // 视频上传
    editor.ui.registry.addButton('videoupload', {
      icon: 'icon-shipin2',
      tooltip: 'add video',
      onAction: function () {
        var fileTypeLimit = (editor.getParam('fileTypeLimit') && editor.getParam('fileTypeLimit')['video']) || "video/*"
        pickFile(editor, fileTypeLimit).then(function (files) {
          if (files.length > 0) {
            uploadFileFun(files[0], 'video')
          }
        });
      }
    });
    // 音频上传
    editor.ui.registry.addButton('audioupload', {
      icon: 'icon-yinpin',
      tooltip: 'add audio',
      onAction: function () {
        var fileTypeLimit = (editor.getParam('fileTypeLimit') && editor.getParam('fileTypeLimit')['audio']) || "audio/*"
        pickFile(editor, fileTypeLimit).then(function (files) {
          if (files.length > 0) {
            uploadFileFun(files[0], 'audio')
          }
        });
      }
    });
  });

}());
